import 'package:flutter/material.dart';
import 'package:flutter_swiper_view/flutter_swiper_view.dart';
// import 'package:flutter_page_indicator/flutter_page_indicator.dart';

/// 方形轮播图分页
class SquareSwiperPagination extends SwiperPlugin {
  /// 当前索引时的颜色, if set null , will be Theme.of(context).primaryColor
  final Color activeColor;

  /// if set null , will be Theme.of(context).scaffoldBackgroundColor
  final Color color;

  /// 激活时的点大小
  final double activeSize;

  /// 点的大小
  final double size;

  /// 点之间的间距
  final double space;

  final Key key;

  const SquareSwiperPagination({
    required this.activeColor,
    required this.color,
    this.key=const Key("pagination_0"),
    this.size=10.0,
    this.activeSize= 10.0,
    this.space= 3.0
  });

  @override
  Widget build(BuildContext context, SwiperPluginConfig config) {
    if (config.itemCount > 20) {
      print("项目计数太大，我们建议在此位置使用FractionPaginationBuilder而不是DotSwiperPaginationBuilder");
    }

    Color activeColor = this.activeColor;
    Color color = this.color;

    if (activeColor == null || color == null) {
      ThemeData themeData = Theme.of(context);
      activeColor = this.activeColor ?? themeData.primaryColor;
      color = this.color ?? themeData.scaffoldBackgroundColor;
    }

    if (config.indicatorLayout != PageIndicatorLayout.NONE && config.layout == SwiperLayout.DEFAULT) {
      if(config.pageController!=null){
        return PageIndicator(
          count: config.itemCount,
          controller: config.pageController!,
          layout: config.indicatorLayout,
          size: size,
          activeColor: activeColor,
          color: color,
          space: space,
          );
      }
    }

    List<Widget> list = [];

    int itemCount = config.itemCount;
    int activeIndex = config.activeIndex;

    for (int i = 0; i < itemCount; ++i) {
      bool active = i == activeIndex;
      list.add(Container(
        key: Key("pagination_$i"),
        margin: EdgeInsets.all(space),
        child: Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(size/2),
            color: active ? activeColor : color,
          ),
          width: active ? activeSize * 2 : size,
          height: active ? activeSize : size,
        ),
      ));
    }

    if (config.scrollDirection == Axis.vertical) {
      return new Column(
        key: key,
        mainAxisSize: MainAxisSize.min,
        children: list,
      );
    } else {
      return new Row(
        key: key,
        mainAxisSize: MainAxisSize.min,
        children: list,
      );
    }
  }
}